package Dao;

import DB.DBConnection;
import pojo.MilkTeaProduct;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MilkDao {

    //
    public static List<MilkTeaProduct> getProducts() {
        List<MilkTeaProduct> products = new ArrayList<>();
        String sql = "SELECT product_id, product_name, price,stock,description FROM milk_tea";
        try (Connection connection = DBConnection.getConnection()) {
            PreparedStatement statement = connection.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
                while (resultSet.next()) {
                    int productId = resultSet.getInt("product_id");
                    String productName = resultSet.getString("product_name");
                    double price = resultSet.getDouble("price");
                    int stock = resultSet.getInt("stock");
                    String description = resultSet.getString("description");
                    products.add(new MilkTeaProduct(productId,productName,price,stock, description));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return products;
        }

    public void addProduct(MilkTeaProduct product) {
        String sql = "INSERT INTO milk_tea (product_name, price ,inprice, stock,description) VALUES (?,?,?,0,?)";
        try (Connection connection = DBConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, product.getProduct_name());
            statement.setDouble(2, product.getPrice());
            statement.setDouble(3, product.getInprice());
            statement.setString(4, product.getDescription());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void deleteProduct(int product_id) {
        String sql = "DELETE FROM milk_tea WHERE product_id =?";
        try (Connection connection = DBConnection.getConnection()) {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setInt(1, product_id);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void updateProduct(MilkTeaProduct product) {
        String sql = "UPDATE milk_tea SET product_name =?, price =?,inprice=?, description =? WHERE product_id =?";
        try (Connection connection = DBConnection.getConnection()) {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, product.getProduct_name());
            statement.setDouble(2, product.getPrice());
            statement.setDouble(3, product.getInprice());
            statement.setString(4, product.getDescription());
            statement.setInt(5, product.getProduct_id());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public MilkTeaProduct getProductById(int product_id) {
        String sql = "SELECT * FROM milk_tea WHERE product_id =?";
        try (Connection connection = DBConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setInt(1, product_id);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                int id = resultSet.getInt("product_id");
                String product_name = resultSet.getString("product_name");
                double price = resultSet.getDouble("price");
                int stock = resultSet.getInt("stock");
                String description = resultSet.getString("description");
                return new MilkTeaProduct(id, product_name,price,stock,description);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //
    public void updateStockQuantity(int product_id, int additionalQuantity) {
        String sql = "UPDATE milk_tea SET stock = stock+? WHERE product_id =?";
        try (Connection connection = DBConnection.getConnection()) {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setInt(1, additionalQuantity);
            statement.setInt(2, product_id);
            statement.executeUpdate();
            System.out.println("进货成功，产品 " + product_id + " 的库存已更新。");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("进货操作出现问题，请检查产品ID是否正确以及数据库连接是否正常。");
        }
    }

}

